*************************************************
*This file cleans robot exposure the state level*
*************************************************

*Exclude observation within CZ. Use shock in state excluding CZ
use "$list/czones_list.dta", clear
merge m:1 statefip using "$list/states_list.dta", keep(3) nogenerate
save "$clean_data_automation/states_exposure_robots.dta", replace

************************************************
* 			 Shock at state level			   *	
************************************************

*Instrument
use "$clean_data_lmarket/state_1970_emp_by_ifr19.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
drop *_us_* *_man_* /*Remove endogenous one*/
collapse (mean) apr_* [w=emppriv], by(statefip) fast
rename apr_* expof_state_*
merge 1:m statefip using "$clean_data_automation/states_exposure_robots.dta", keep(3) nogenerate //in 1970 Census, states of Delaware, Idaho, Montana, North Dakota, South Dakota, Vermont and Wyoming are missing
save "$clean_data_automation/states_exposure_robots.dta", replace

**OLS measure (employment shares in 1990)**
use "$clean_data_lmarket/state_1990_emp_by_ifr19.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
keep statefip emppriv *_us_* /*Keep endogenous one*/
collapse (mean) apr_us* [w=emppriv], by(statefip) fast
rename apr_* expof_state_*
merge 1:m statefip using "$clean_data_automation/states_exposure_robots.dta", keep(3) nogenerate 
*CZ 20901 is only labor market in state (CT: 9), so value missing here, since no other labor market in same state
save "$clean_data_automation/states_exposure_robots.dta", replace

************************************************
* 		Shock at state level excluding CZ	   *	
************************************************

*Instrument
use "$clean_data_lmarket/czone_1970_emp_by_ifr19_noCZ_shockState.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
drop *_us_* *_man_* /*Remove endogenous one*/
collapse (mean) apr_* [w=emppriv_noCZ_shockState], by(czone) fast
rename apr_* expof_state_noCZ_*
merge 1:1 czone using "$clean_data_automation/states_exposure_robots.dta", keep(1 3) nogenerate 
save "$clean_data_automation/states_exposure_robots.dta", replace

**OLS measure (employment shares in 1990)**
use "$clean_data_lmarket/czone_1990_emp_by_ifr19_noCZ_shockState.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
keep czone emppriv_noCZ_shockState *_us_* /*Keep endogenous one*/
collapse (mean) apr_us* [w=emppriv_noCZ_shockState], by(czone) fast
rename apr_* expof_state_noCZ_*
merge 1:1 czone using "$clean_data_automation/states_exposure_robots.dta", assert(2 3) nogenerate 
*CZ 20901 is only labor market in state (CT: 9), so value missing here, since no other labor market in same state

*States 09 and 44 have only one CZ, so employment shares should be . if CZ excluded. Correct manually because of rounding errors
foreach var of varlist *state_noCZ* {
replace `var' = . if statefip==9|statefip==44
}

save "$clean_data_automation/states_exposure_robots.dta", replace

drop *euro4* *euro5* *euro8* *04_14* *93_14* *_lv*

cap drop tempid			
save "$clean_data_automation/states_exposure_robots.dta", replace

